package com.xdtech.forum.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xdtech.forum.model.Article;
import com.xdtech.forum.model.Category;
import com.xdtech.forum.model.Member;
import com.xdtech.forum.service.ArticleService;
import com.xdtech.forum.service.CategoryService;
import com.xdtech.forum.vo.ArticleItem;
import com.xdtech.forum.vo.CategoryItem;
import com.xdtech.forum.vo.MemberItem;
import com.xdtech.web.model.Pagination;
import com.xdtech.web.model.ResultMessage;

/**
 * 
 * @author max.zheng
 * @create 2014-12-06 20:53:58
 * @since 1.0
 * @see
 */
@Controller
@Scope("prototype")
public class ArticleController {
	@Autowired
	private ArticleService articleService;
	@Autowired
	private CategoryService categoryService;
	@RequestMapping(value="/article.do",params = "article")
	public ModelAndView skipArticle() {
		return new ModelAndView("forum/article/article_ftl");
	}
	
	@RequestMapping(value={"/article.do","/article.action"},params="loadList")
	@ResponseBody
	public Map<String, Object> loadList(HttpServletRequest request,Pagination pg) {
		Map<String, Object> results = null;
		if (StringUtils.isEmpty(request.getParameter("page"))) {
			//不分页处理
			results = articleService.loadPageAndCondition(null, null);
		}else {
			results = articleService.loadPageAndCondition(pg, null);
		}
		
		return results;
	}
	
	@RequestMapping(value="/article.do",params = "editArticle")
	public ModelAndView editArticle(HttpServletRequest request,Long articleId) {
		List<CategoryItem> categoryItems = categoryService.loadCategoryItems();
		request.setAttribute("categoryItems", categoryItems);
		if (articleId!=null) {
			request.setAttribute("articleItem", articleService.loadArticleItem(articleId));
		}
		return new ModelAndView("forum/article/editArticle_ftl");
	}
	
	@RequestMapping(value="/article.do",params = "saveArticle")
	@ResponseBody
	public ResultMessage saveArticle(ArticleItem item) {
		ResultMessage r = new ResultMessage();
		if (articleService.saveOrUpdateArticle(item)) {
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/article.do",params = "deleteArticleItems")
	@ResponseBody
	public ResultMessage deleteArticleItems(String ids) {
		ResultMessage r = new ResultMessage();
		if (StringUtils.isNotEmpty(ids)) {
			String[] tempIds = ids.split(",");
			List<Long> articleIds = new ArrayList<Long>();
			for (String id : tempIds) {
				articleIds.add(Long.valueOf(id));
			}
			articleService.deleteArticleInfo(articleIds);
			r.setSuccess(true);
		}else {
			r.setSuccess(false);
		}
		return r;
	}
	
	//以下是show模块方法
	@RequestMapping(value="/article.action",params = "postArticle")
	@ResponseBody
	public ResultMessage postArticle(ArticleItem item,HttpServletRequest request) {
		ResultMessage r = new ResultMessage();
		MemberItem memberItem = (MemberItem) request.getSession().getAttribute("member");
		if (memberItem!=null) {
			Article article = new Article();
			article.setTitle(item.getTitle());
			article.setContent(item.getContent());
			article.setMember(new Member(memberItem.getId()));
			article.setCategory(new Category(1L));
			articleService.save(article);
		}else {
			r.setMsg("未登录，无法提交");
			r.setSuccess(false);
		}
		return r;
	}
	
	@RequestMapping(value="/article.action",params = "myArticles")
	public ModelAndView myArticles(HttpServletRequest request) {
		MemberItem memberItem = (MemberItem) request.getSession().getAttribute("member");
		List<ArticleItem> articles = articleService.loadArticleItemsByMemberId(memberItem.getId());
		request.setAttribute("articles", articles);
		return new ModelAndView("forum/portal/myArticles_ftl");
	}
	
	@RequestMapping(value="/article.action",params = "loadArticlesByCtg")
	public ModelAndView loadArticlesByCtg(HttpServletRequest request,Long ctgId) {
		
		Map<String, Object> rs = articleService.loadPageByTypeAndKey(new Pagination(),ctgId.toString(),null);
		List<ArticleItem> articles = (List<ArticleItem>) rs.get("rows");
		request.setAttribute("articles", articles);
		CategoryItem categoryItem = categoryService.loadCategoryItem(ctgId);
		request.setAttribute("category", categoryItem);
		return new ModelAndView("forum/portal/articlesByCtg_ftl");
	}
	@RequestMapping(value="/article.action",params = "loadArticleContent")
	public ModelAndView loadArticleContent(HttpServletRequest request,Long articleId) {
		ArticleItem articleItem = articleService.loadArticleItem(articleId);
		request.setAttribute("articleItem", articleItem);
		return new ModelAndView("forum/portal/articleContent_ftl");
	}
	
	@RequestMapping(value={"/article.action"},params="loadListByType")
	@ResponseBody
	public Map<String, Object> loadListByType(HttpServletRequest request,Pagination pg,String type,String keyword) {
		Map<String, Object> results = null;
		results = articleService.loadPageByTypeAndKey(pg, type,keyword);
		return results;
	}
	/**
	 * 根据关键字查询
	 * 
	 * @author <a href="max.zheng@zkteco.com">郑志雄</>
	 * @since 2015-3-10 下午4:39:37
	 * @param request
	 * @param articleId
	 * @return
	 */
	@RequestMapping(value="/searchArticle.action")
	public ModelAndView loadArticleContent(HttpServletRequest request,String keyword,Pagination pg) {
		Map<String, Object> rs = articleService.loadPageByTypeAndKey(pg,null,keyword);
		List<ArticleItem> articles = (List<ArticleItem>) rs.get("rows");
		request.setAttribute("articles", articles);
		request.setAttribute("keyword", keyword);
		return new ModelAndView("forum/portal/articlesByKey_ftl");
	}

}
